package com.efounder.form.comp.multiple_images_selector;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.form.DMColComponent;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.comp.IUIComponent;
import com.efounder.frame.ViewSize;
import com.efounder.mobilecomps.R;
import com.efounder.util.AppContext;
import com.pizidea.imagepicker.AndroidImagePicker;
import com.pizidea.imagepicker.bean.ImageItem;

import java.util.List;

/**
 * Created by lch on 16/9/20.
 */
	public class ImageSelectorGrid extends GridView implements IComponent,IUIComponent,DMComponent,DMColComponent {



	private GridAdapter adapter;

	private IUIComponent parentComp;


	public ImageSelectorGrid() {
		super(AppContext.getInstance());
	}

	public ImageSelectorGrid(Context context) {
		super(context);
	}

	public void dataChanged(EFDataSet dataSet) {


	}
	@Override
	public DataSetComponent getDataSetComponent() {
		return dataSetComponent;
	}

	@Override
	public void setDataSetComponent(DataSetComponent dsc) {
		if (dataSetComponent != null)
			dataSetComponent.removeDMComponent(this);
		dataSetComponent = dsc;
		if (dataSetComponent != null)
			dataSetComponent.insertDMComponent(this);
	}

	@Override
	public void setLayout() {
		this.setLayoutGravity();
		this.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,dp2px(350)));
		this.setHorizontalSpacing(dp2px(3));
		this.setVerticalSpacing(dp2px(5));
	}

	@Override
	public void creationComplete() {
		//this.setLayoutSize();



		//初始化样式
		this.setSelector(new ColorDrawable(Color.TRANSPARENT));
		this.setNumColumns(4);
		this.setVerticalScrollBarEnabled(false);
		adapter = new GridAdapter(AppContext.getInstance());
		adapter.update();
		this.setAdapter(adapter);
		this.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
									long arg3) {
				if (arg2 == Bimp.tempSelectBitmap.size()) {
					AndroidImagePicker.getInstance().pickMulti(AppContext.getInstance(), true, new AndroidImagePicker.OnImagePickCompleteListener() {
						@Override
						public void onImagePickComplete(List<ImageItem> items) {
							if(items != null && items.size() > 0){
								Bimp.tempSelectBitmap.addAll(items);
							}

							adapter.update();

						}
					});

				} else {
					//TODO 此处处理 删除图片的逻辑，要注意处理处理好回调。
				}
			}
		});


	}

	/*
    * LinearLayout大小
    */
	public void setLayoutSize() {
		//设置控件宽高
		IUIComponent parent = (IUIComponent)this.getParent();
		int parentWidth = parent.getViewSize().getWidth();
		int parentHeight = parent.getViewSize().getHeight();

		int layoutWidth = parentWidth * this.percentWidth / 100;
		int layoutHeight = parentHeight * this.percentHeight / 100;

		layoutWidth = layoutWidth == 0 ? this.width : layoutWidth;
		layoutHeight = layoutHeight == 0 ? this.height : layoutHeight;

		this.size.setWidth(layoutWidth);
		this.size.setHeight(layoutHeight);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(layoutWidth, layoutHeight);
		layoutParams.rightMargin = parent.getGap();
		layoutParams.bottomMargin = parent.getGap();

		this.setLayoutParams(layoutParams);
	}

	public void setLayoutGravity() {
		int hAlign;
		int vAlign;

		if(horizontalAlign == null){
			hAlign = Gravity.CENTER;
		}
		else if (horizontalAlign.equals("left")) {
			hAlign = Gravity.LEFT;
		} else if (horizontalAlign.equals("right")) {
			hAlign = Gravity.RIGHT;
		} else {
			hAlign = Gravity.CENTER;
		}

		if(verticalAlign == null) {
			vAlign = Gravity.BOTTOM;
		}
		else if (verticalAlign.equals("top")) {
			vAlign = Gravity.TOP;
		} else if (verticalAlign.equals("middle")) {
			vAlign = Gravity.CENTER_VERTICAL;
		} else{
			vAlign = Gravity.BOTTOM;
		}


		this.setGravity(hAlign | vAlign);
	}





	public String getHorizontalAlign() {
		return horizontalAlign;
	}

	public void setHorizontalAlign(String horizontalAlign) {
		this.horizontalAlign = horizontalAlign;
	}

	public String getVerticalAlign() {
		return verticalAlign;
	}

	public void setVerticalAlign(String verticalAlign) {
		this.verticalAlign = verticalAlign;
	}

	@Override
	public void setDataSetID(String dataSetID) {
		this.dataSetID = dataSetID;
	}

	@Override
	public String getDataSetColID() {
		return dataSetColID;
	}

	@Override
	public void setDataSetColID(String dataSetColID) {
		this.dataSetColID = dataSetColID;
	}

	@Override
	public String getDataSetNameColID() {
		return null;
	}

	@Override
	public void setDataSetNameColID(String value) {

	}

	@Override
	public String getInternalDataSetID() {
		return null;
	}

	@Override
	public void setInternalDataSetID(String dataSetID) {

	}

	@Override
	public void setViewDataSetID(String viewDataSetID) {

	}

	@Override
	public String getViewDataSetID() {
		return null;
	}

	@Override
	public void setViewDataSetColID(String viewDataSetColID) {

	}

	@Override
	public String getViewDataSetColID() {
		return null;
	}

	@Override
	public void setValueDataSetColID(String valueDataSetColID) {

	}

	@Override
	public String getValueDataSetColID() {
		return null;
	}

	@Override
	public Boolean getIsUserInternalDataSetID() {
		return null;
	}

	@Override
	public void setIsUserInternalDataSetID(Boolean v) {

	}

	@Override
	public String getIdentifier() {
		return null;
	}

	@Override
	public Object getPropertyMap() {
		return null;
	}

	@Override
	public void setPropertyMap(Object value) {

	}

	@Override
	public Object getProperty(String key, Object defVal) {
		return null;
	}

	@Override
	public void setProperty(String key, Object value) {

	}

	@Override
	public int getNumberPrecision() {
		return 0;
	}

	@Override
	public void seNumberPrecision(int value) {

	}

	@Override
	public String getNumberFormat() {
		return null;
	}

	@Override
	public void setNumberFormat(String numberFormat) {

	}

	@Override
	public String getColumnType() {
		return null;
	}

	@Override
	public void setColumnType(String value) {

	}

	@Override
	public String getEditorType() {
		return null;
	}

	@Override
	public void setEditorType(String value) {

	}
	@Override
	public String getDateFormat() {
		return null;
	}

	@Override
	public void setDateFormat(String dateFormat) {

	}

	@Override
	public String getFormulaOne() {
		return null;
	}

	@Override
	public void setFormulaOne(String formulaOne) {

	}



	@Override
	public String getFkeyID() {
		return null;
	}

	@Override
	public void setFkeyID(String fkey) {

	}

	@Override
	public String getRlglID() {
		return null;
	}

	@Override
	public void setRlglID(String rlglID) {

	}

	@Override
	public String getDataSetID() {
		return dataSetID;
	}

	@Override
	public void setDataSet(EFDataSet dataSet) {
		this.dataSet = dataSet;

		this.dataChanged(dataSet);
	}

	@Override
	public EFDataSet getDataSet() {
		return dataSet;
	}

	@Override
	public EFRowSet getMainRowSet() {
		return mainRowSet;
	}

	@Override
	public void dataSetChanged(DataSetEvent event) {
		dataChanged(event.getDataSet());
	}

	@Override
	public String getID() {
		return id;
	}

	@Override
	public void setID(String id) {
		this.id = id;
	}

	@Override
	public int getGap() {
		return gap;
	}

	@Override
	public void setGap(int gap) {
		this.gap = gap;
	}


	@Override
	public ViewSize getViewSize() {
		return size;
	}

	@Override
	public void setViewSize(ViewSize size) {
		this.size = size;
	}

	@Override
	public String getEditMask() {
		return null;
	}

	@Override
	public void setEditMask(String editMask) {
	}
	@Override
	public void setCompWidth(int width) {
		this.width = width;
	}

	@Override
	public int getCompWidth(){
		return this.width;
	}

	@Override
	public void setCompHeight(int height) {
		this.height = height;
	}

	@Override
	public int getCompHeight() {
		return this.height;
	}
	@Override
	public void setPercentWidth(int percentW) {
		percentWidth = percentW;
	}

	@Override
	public int getPercentHeight() {
		return percentHeight;
	}

	@Override
	public void setPercentHeight(int percentH) {
		percentHeight = percentH;
	}

	//-------------------------------------------------------
	// 基本属性
	//-------------------------------------------------------
	private String id;
	private float x = 0;
	private float y = 0;
	private int width;
	private int height;
	private Boolean visible;
	private int tabIndex = 0;
	private int fontColor = 0;
	private int fontSize = 12;

	private Boolean editable;
	private String editMask;
	/**
	 * 布局类型：垂直vertical、水平horizontal
	 */
	private String layoutType = "horizontal";
	private int requestColCount;
	private double horizontalGap;
	private double verticalGap;

	private int percentWidth;

	private int percentHeight;

	private int gap = 0;

	private ViewSize size = new ViewSize(0,0);

	private String dataSetID;
	private String dataSetColID;

	private String horizontalAlign;
	private String verticalAlign;

	private DataSetComponent dataSetComponent;
	private EFDataSet dataSet;
	private EFRowSet mainRowSet;

	public int getPercentWidth() {
		return percentWidth;
	}

	public void setId(String id) {
		this.id = id;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}



	public Boolean getVisible() {
		return visible;
	}

	public void setVisible(Boolean visible) {
		this.visible = visible;

	}

	public int getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(int tabIndex) {
		if(tabIndex != -1) {
			this.tabIndex = tabIndex;
		}

	}


	public Boolean getEditable() {
		return editable;
	}

	public void setEditable(Boolean editable) {
		this.editable = editable;
	}

	public String getLayoutType() {
		return layoutType;
	}

	public void setLayoutType(String layoutType) {
		this.layoutType = layoutType;
	}

	public int getRequestColCount() {
		return requestColCount;
	}

	public void setRequestColCount(int requestColCount) {
		this.requestColCount = requestColCount;
	}

	public double getHorizontalGap() {
		return horizontalGap;
	}

	public void setHorizontalGap(double horizontalGap) {
		this.horizontalGap = horizontalGap;
	}

	public double getVerticalGap() {
		return verticalGap;
	}

	public void setVerticalGap(double verticalGap) {
		this.verticalGap = verticalGap;
	}

	public int getFontColor() {
		return fontColor;
	}
	public void setFontColor(int fontColor) {
		this.fontColor = fontColor;
	}
	public int getFontSize() {
		return fontSize;
	}
	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}


	//临时方法。
	public int dp2px(float dp) {
		final float scale = getResources().getDisplayMetrics().density;
		return (int)(dp * scale + 0.5f);
	}
	public class GridAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private int selectedPosition = -1;
		private boolean shape;

		public boolean isShape() {
			return shape;
		}

		public void setShape(boolean shape) {
			this.shape = shape;
		}

		public GridAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		public void update() {
			loading();
		}

		public int getCount() {
			if(Bimp.tempSelectBitmap.size() == 9){
				return 9;
			}
			return (Bimp.tempSelectBitmap.size() + 1);
		}

		public Object getItem(int arg0) {
			return null;
		}

		public long getItemId(int arg0) {
			return 0;
		}

		public void setSelectedPosition(int position) {
			selectedPosition = position;
		}

		public int getSelectedPosition() {
			return selectedPosition;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_published_grida,
						parent, false);
				holder = new ViewHolder();
				holder.image = (ImageView) convertView
						.findViewById(R.id.item_grida_image);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			if (position ==Bimp.tempSelectBitmap.size()) {
				holder.image.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.icon_addpic_unfocused));
				if (position == 9) {
					holder.image.setVisibility(View.GONE);
				}
			} else {
				holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());
			}

			return convertView;
		}

		public class ViewHolder {
			public ImageView image;
		}

		Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
					case 1:
						adapter.notifyDataSetChanged();
						break;
				}
				super.handleMessage(msg);
			}
		};

		public void loading() {
			new Thread(new Runnable() {
				public void run() {
					while (true) {
						if (Bimp.max == Bimp.tempSelectBitmap.size()) {
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
							break;
						} else {
							Bimp.max += 1;
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
						}
					}
				}
			}).start();
		}
	}
	@Override
	public IUIComponent getParentComp() {
		return parentComp;
	}

	@Override
	public void setParentComp(IUIComponent comp) {
		parentComp = comp;
	}
}